package com.wlanauth.dao.manage.impl;

import java.io.Serializable;
import java.util.List;

import org.springframework.dao.DataAccessException;

import com.wlanauth.dao.common.impl.CommonDaoImpl;
import com.wlanauth.dao.manage.OperatorDao;
import com.wlanauth.domain.manage.Operator;

public class OperatorDaoImpl extends CommonDaoImpl<Operator> implements
		OperatorDao {

	public Operator getByUsername(String username) throws DataAccessException {
		String hql = "from Operator o where o.username='%s'";
		@SuppressWarnings("unchecked")
		List<Operator> list = this.getHibernateTemplate().find(String.format(hql, username));
		if(list == null || list.size()<1) return null;
		if(list.size()>1){
			throw new RuntimeException("业务数据错误!期待返回最多1个结果,实际返回结果数:"+list.size());
		}
		Operator oper = list.get(0);
		//this.getHibernateTemplate().initialize(oper.getRole());
		if(oper.getRole()!=null){
			this.getHibernateTemplate().initialize(oper.getRole().getSystemResources());
		}
		return oper;
	}

	@Override
	public Operator get(Serializable key) throws DataAccessException {
		Operator oper =  super.get(key);
		if(oper!=null){
			this.getHibernateTemplate().initialize(oper.getRole());
		}
		return oper;
	}
	
	
	

}
